package com.amlyc.syn;

public class DeadLock {
    public static void main(String[] args) {
        Makeup g1 = new Makeup(0, "g1");
        Makeup g2 = new Makeup(1, "g2");
        new Thread(g1).start();
        new Thread(g2).start();
    }
}

class LipStick {

}

class Mirror {

}

class Makeup implements Runnable {
    static LipStick lipStick = new LipStick();
    static Mirror mirror = new Mirror();
    int choice;
    String name;
    Makeup(int choice,String name){
        this.choice = choice;
        this.name = name;
    }
    @Override
    public void run() {
        try {
            makeUp();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
    private void makeUp() throws InterruptedException {
        if (choice == 0) {
            synchronized (lipStick) {
                System.out.println(this.name + "获得口红的锁");
                Thread.sleep(1000);


            }
            synchronized (mirror) {
                System.out.println(this.name+"获得镜子的锁");
            }
        }else {
            synchronized (mirror) {
                System.out.println(this.name + "获得镜子的锁");
                Thread.sleep( 1000);

            }
            synchronized (lipStick) {
                System.out.println(this.name+"获得口红的锁");
            }
        }

    }
}
